package com.basker.pisces.exception;

import org.springframework.util.StringUtils;

import com.basker.pisces.resource.StringResources;

/**
 * 平台基础运行时异常类，表示平台代码执行过程中:
 * <p>
 * 1.抛出的异常 2.封装的非runtime异常 *
 * <p>
 * 其中code作为一个异常的标识，通常是：系统.模块.类.方法-序号，如果在{@link StringResources}中存在该key,则异常消息为该key对应的值
 *
 * @author hangwen
 * @see ExceptionFactory
 */
public class ApplicationRuntimeException extends RuntimeException {
    private static final long serialVersionUID = -4437937033738785841L;

    private String code;

    public ApplicationRuntimeException(String code, String msg) {
        super(msg);
        this.code = code;
    }

    public ApplicationRuntimeException(String code, String msg, Throwable cause) {
        super(msg, cause);
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    @Override
    public String toString() {
        String code = this.code;

        if (StringUtils.isEmpty(code)) {
            return super.toString();
        }

        String name = getClass().getName();
        String message = getLocalizedMessage();

        String s = name + ": " + code;

        return (message != null) ? (s + ", " + message) : s;
    }

}
